package brcl.view;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * 统一封装返回model
 *
 * @version v1.0
 * @className: ServerResponse
 * @author: heyiheng
 * @date: 2020-11-28 09:03
 */
@Data
public class ServerResponse<T> implements Serializable {

	@ApiModelProperty(value = "状态[成功：1000，失败：1001]")
	private int status;
	@ApiModelProperty(value = "提示信息")
	private String msg;
	@ApiModelProperty(value = "返回数据")
	private T data;

	private ServerResponse(int status){
		this.status = status;
	}

	private ServerResponse(int status, T data){
		this.status = status;
		this.data = data;
	}

	private ServerResponse(int status, String msg, T data){
		this.status = status;
		this.msg = msg;
		this.data = data;
	}

	private ServerResponse(int status, String msg){
		this.status = status;
		this.msg = msg;
	}


	//使之不在json序列化结果当中
	@JsonIgnore
	public boolean isSuccess(){
		return this.status == 1000;
	}

	public int getStatus(){
		return status;
	}

	public T getData(){
		return data;
	}

	public String getMsg() {
		return msg;
	}


	public static <T> ServerResponse<T> createBySuccess(){
		return new ServerResponse<T>(1000);
	}

	public static <T> ServerResponse<T> createBySuccessMessage(String msg){
		return new ServerResponse<T>(1000,msg);
	}

	public static <T> ServerResponse<T> createBySuccess(T data){
		return new ServerResponse<T>(1000,data);
	}

	public static <T> ServerResponse<T> createBySuccess(String msg,T data){
		return new ServerResponse<T>(1000,msg,data);
	}

	public static <T> ServerResponse<T> createByError(){
		return new ServerResponse<T>(1001,"未知错误");
	}

	public static <T> ServerResponse<T> createByErrorMessage(String errorMessage){
		return new ServerResponse<T>(1001,errorMessage);
	}

	public static <T> ServerResponse<T> createByErrorCodeMessage(int errorCode,String errorMessage){
		return new ServerResponse<T>(errorCode,errorMessage);
	}
}
